﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.Guids;
using Volo.Abp.MultiTenancy;

namespace Himp.DataHub
{
    /// <summary>
    /// 科室信息
    /// </summary>
    public class DeptInfo : FullAuditedAggregateRoot<Guid>, IMultiTenant
    {
        /// <summary>
        /// 科室编码
        /// </summary>
        public string DeptCodg { get; set; }

        /// <summary>
        /// 科室名称
        /// </summary>
        public string DeptName { get; set; }

        /// <summary>
        /// 拼音助记码
        /// </summary>
        public string? Pinyincode { get; set; }

        /// <summary>
        /// 五笔助记码
        /// </summary>
        public string? Wubicode { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        public int Srtno { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? Begntime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        public DateTime? Endtime { get; set; }

        /// <summary>
        /// 简介
        /// </summary>
        public string? Itro { get; set; }

        /// <summary>
        /// 科别
        /// 参照科室代码
        /// </summary>
        public string? CatyCode { get; set; }

        /// <summary>
        /// 科别
        /// 参照科室代码
        /// </summary>
        public string? CatyName { get; set; }

        /// <summary>
        /// 机构信息Id
        /// </summary>
        public Guid? OrganId { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string? Memo { get; set; }

        /// <summary>
        /// 等级序号
        /// </summary>
        public int? LvIndex { get; set; }

        /// <summary>
        /// 科室对应属性
        /// </summary>
        public List<DeptAttr> DeptAssocAttrs { get; set; }

        /// <summary>
        /// 科室病区
        /// </summary>
        public List<DeptWard> DeptWards { get; set; }

        /// <summary>
        /// 科室病区
        /// </summary>
        public List<DeptConsultRm> DeptConsultRms { get; set; }

        /// <summary>
        /// 上级科室
        /// </summary>
        public Guid? ParentId { get; set; }

        /// <summary>
        /// 上级科室
        /// </summary>
        public DeptInfo? Parent { get; set; }

        /// <summary>
        /// 下级科室
        /// </summary>
        public List<DeptInfo> Children { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Guid? TenantId { get; set; }
        

        public DeptInfo()
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="hospDeptCodg"></param>
        /// <param name="hospDeptName"></param>
        /// <param name="caty"></param>
        /// <param name="sort"></param>
        public DeptInfo(Guid? parentId, string hospDeptCodg, string hospDeptName, string catyCode, string catyName,
             Guid? organId, int sort, DateTime? begntime, DateTime? endtime, string itro)
        {
            ParentId = parentId;
            DeptCodg = hospDeptCodg;
            DeptName = hospDeptName;
            CatyCode = catyCode;
            CatyName = catyName;
            OrganId = organId;
            Srtno = sort;
            Begntime = begntime;
            Endtime = endtime;
            Itro = itro ?? string.Empty;
            Pinyincode = string.Empty;
            Wubicode = string.Empty;
            DeptAssocAttrs = new List<DeptAttr>();
            DeptWards = new List<DeptWard>();
            DeptConsultRms = new List<DeptConsultRm>();
        }

        public void ChangeDept(string deptName, string catyCode, string catyName, Guid? organId, int srtNo, DateTime? begntime, DateTime? endtime, string itro)
        {
            DeptName = deptName;
            CatyCode = catyCode;
            CatyName = catyName;
            OrganId = organId;
            Srtno = srtNo;
            Begntime = begntime;
            Endtime = endtime;
            Itro = itro;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="deptmentPropertyId"></param>
        public void SetDeptmentProperty(IGuidGenerator guidGenerator, List<Guid> deptAttrIdList)
        {
            //DeptAssocAttrs.RemoveAll(d => !deptAttrIdList.Contains(d.DeptAttrId));

            //foreach (var deptAttrId in deptAttrIdList)
            //{
            //    if (!DeptAssocAttrs.Any(d => d.DeptAttrId == deptAttrId))
            //    {
            //        DeptAssocAttrs.Add(new DeptAttr(guidGenerator.Create(), deptAttrId));
            //    }
            //}
        }
    }
}
